<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <style>
      .box{
            width: 100px;
            height: 100px;
            /* position: absolute;
            left:0;
            top:0; */
            background-color: pink;
            /* border: none; */
            border-radius: 50%; 
        }
    </style>
  </head>
  <body>
    <div class="box"></div>
    <script>
      // window.onload=function(){ //等待浏览器加载完毕
      let box=document.querySelector(".box");//获取dom节点 ".box"(因为我的id是box)
      let moveX = 0;
      let moveY = 0;
      let startX = 0;
      let startY = 0;
      let stopX = 0;
      let stopY = 0;
      //记录点击球的开始坐标
      box.addEventListener("touchstart",function(event){
        startX = event.touches[0].pageX;
        startY = event.touches[0].pageY;
      });
      //小球移动
      box.addEventListener("touchmove",function(event){
        //当前小球停留的坐标+手指移动的坐标-小球刚开始触摸的坐标
        moveX =stopX + event.touches[0].pageX - startX;
        moveY =stopY + event.touches[0].pageY - startY;
        box.style.transform = `translate(${moveX}px,${moveY}px)`;

      });

      //球停止移动的时候需要记录当前移动的位置
      box.addEventListener("touchend",function(){
        //保留移动之后结束的坐标
        stopX = moveX;
        stopY = moveY;
      });
    </script>
  </body>
</html>